#coding=utf-8
__author__ = 'wwj'
from pyquery import PyQuery as pq
import sys
import json
import re
import requests as req
import time
import re
import MySQLdb as db

class drag():
    def __init__(self):
        headers = {
        'Referer': 'https://partsouq.com/',
        'Connection': 'Keep-Alive',
        'Accept-Language': 'en-US,en;q=0.8,zh-Hans-CN;q=0.5,zh-Hans;q=0.3',
        'Accept': 'text/html, application/xhtml+xml, */*',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.132 Safari/537.36'
        }
        self.url = 'https://partsouq.com/en/catalog/genuine/locate?c=Mazda'
        session = req.session()
        session.get('https://partsouq.com/currencymanager/default/setcurrency/currency/CNY',headers = headers)
        back_data = session.get(self.url,headers = headers)
        self.data = pq(back_data.text)
        self.domain = "https://partsouq.com/";

    def page(self):
        url = []
        urlData = []
        a = self.data
        i = 0
        for x in a('.accordion-toggle').items():
            url.append({'name':x.text(),'url':self.domain + x.attr('href')})
        
        for y in url:
            print (self.domain + y['url'])
            b = pq(y['url'])
            dataList = b('.table.table-hover.mb-0px tr:gt(0)')
            for x in dataList.items():
                year = x('[data-title="ModelYear"]').text()
                try:
                    year = int(year)
                except Exception:
                    year = x('[data-title="Datefrom"]').text()[0:4]
                    year = int(year)

                if int(year) < 2000:
                    continue               
                
                name = x('[data-title="Name"]').text()
                CarLine = x('[data-title="CarLine"]').text()
                DestinationRegion = x('[data-title="DestinationRegion"]').text()
                Drive = x('[data-title="Drive"]').text()
                CatalogNo = x('[data-title="CatalogNo"]').text()
                ModelYear = x('[data-title="ModelYear"]').text()
                Datefrom = x('[data-title="Datefrom"]').text()
                urlPage = x('[data-title="Name"] a').attr('href')
                line = {'name':name,'Drive':Drive,'DestinationRegion':DestinationRegion,
                'CarLine':CarLine,'CatalogNo':CatalogNo,'ModelYear':ModelYear,
                'Datefrom':Datefrom,'url':self.domain + urlPage
                }
                mainPart = []
                time.sleep(5)
                print (self.domain + urlPage)
                c = pq(self.domain + urlPage)
                for z in c('.simple-drop').items():
                    tmpName = z.children('.s-h:eq(0)').text()
                    tmpname = z.children('.s-h:eq(0)').children('span').text()
                    tmpName = tmpName.replace(tmpname,'').strip()
                    # import ipdb;ipdb.set_trace()
                    try:
                        di = pq(self.domain + z.children('.s-h:eq(0)').children('a').attr('href'))
                        print (self.domain + z.children('.s-h:eq(0)').children('a').attr('href'))
                    except Exception:
                        di = c
                    tmp = {tmpName:[]}
                    
                    for x in di('.col-sm-3.col-xs-6').items():
                        name = x.text()
                        # import ipdb;ipdb.set_trace()
                        time.sleep(2)
                        e = pq(self.domain + x('a').attr('href'))
                        print (self.domain + x('a').attr('href'))
                        img = e('img.drag').attr('src')
                        # y.update({'lname':{}})
                        sonPart = []
                        for x in e('.part-search-tr').items():
                            Number = x('a').text()
                            Name = x('td:eq(1)').text()
                            Code = x('td:eq(2)').text()
                            note = x('td:eq(3)').text()
                            Qty  = x('td:eq(4)').text()
                            dcode  = x('td:eq(5)').text()
                            sonPart.append({'Number':Number,'Name':Name,'Code':Code,'note':note,
                            'Qty':Qty,'dcode':dcode
                            })
                        img  = img or ''
                        
                        tmp[tmpName].append({'sonPart':sonPart,'img':self.domain + img,'name':name})
                        # tmp = {tmpName:{'sonPart':sonPart,'img':self.domain + img,'name':name}}
                    
                        i += 1
                        print (i)
                        if i > 23:
                            break
                    mainPart.append(tmp)
                    if i > 23:
                        break       
                line['mainPart'] = mainPart
                y.update({'chexi':line})
                urlData.append(y)

                if i > 23:
                    break
            if i > 23:
                break
        # import ipdb;ipdb.set_trace()
        with open('/Users/wwj/Documents/chetongyun/data','w+') as w:
            w.write(json.dumps(urlData))


if __name__ == '__main__':
    obj = drag()
    obj.page()